package com.example.a053textview_button_edittext_toast;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    // 声明对象
    Button btnFirst, btnSecond, btnFly;
    TextView txtSecond, txtThird, txtInform;
    EditText editInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 将资源与变量联系起来
        btnFirst = (Button) findViewById(R.id.btnFirst);
        btnSecond = (Button) findViewById(R.id.btnSecond);
        btnFly = (Button) findViewById(R.id.btnFly);
        txtSecond = (TextView) findViewById(R.id.txtSecond);
        txtThird = (TextView) findViewById(R.id.txtThird);
        txtInform = (TextView) findViewById(R.id.txtInform);
        editInput = (EditText) findViewById(R.id.editInput);

        // 设置监听事件 下面实现接口,不同的按钮有不同的动作
        btnFirst.setOnClickListener(new mClick());
        btnSecond.setOnClickListener(new mClick());
        btnFly.setOnClickListener(new mClick());
    }

    // 自定义点击事件监听器
    class mClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == btnFirst) {
                // 显示第二个按钮和第二个 TextView
                btnSecond.setVisibility(View.VISIBLE);
                txtSecond.setVisibility(View.VISIBLE);
            } else if (v == btnSecond) {
                // 设置第二个 TextView 的随机颜色
                Random random = new Random();
                int r = random.nextInt(255);
                int g = random.nextInt(255);
                int b = random.nextInt(255);
                txtSecond.setTextColor(Color.rgb(r, g, b));

                // 显示第三个 TextView 和其他组件
                txtThird.setVisibility(View.VISIBLE);
                txtInform.setVisibility(View.VISIBLE);
                editInput.setVisibility(View.VISIBLE);
                btnFly.setVisibility(View.VISIBLE);
            } else if (v == btnFly) {
                // 获取 EditText 的输入内容
                String word = editInput.getText().toString();

                // 创建一个自定义 Toast提示信息
                Toast toast = new Toast(MainActivity.this);
                toast.setGravity(Gravity.CENTER, 0, 0);

                // 创建一个 ImageView 用于显示图片
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(R.drawable.broadcast);

                // 创建一个 TextView 用于显示输入内容
                TextView textView = new TextView(MainActivity.this);
                textView.setText(word);

                // 创建一个线性布局容器，将图片和文本加入
                LinearLayout newView = new LinearLayout(MainActivity.this);
                newView.addView(imageView);
                newView.addView(textView);

                // 设置 Toast 的自定义视图
                toast.setView(newView);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.show();
            }
        }
    }
}